/*
 * Author Josinho Joseph J
 */
package com.sdi.columbus;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends Activity  {
/** Called when the activity is first created. */
    private Button btnLogin;
   
    String result;
    EditText et_username;
    EditText et_password;
    EditText et_accountID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
    	
        
        this.btnLogin = (Button)this.findViewById(R.id.button1);
        this.btnLogin.setOnClickListener(new OnClickListener() {
			 
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				//
				btnLogin.setBackgroundColor(Color.parseColor("#4099ff"));
				if(et_accountID.getText().toString().equals("")||et_username.getText().toString().equals("")||et_password.getText().toString().equals(""))
	        	{
	        		  AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
	        	      alertDialog.setTitle("Error");
	        	      alertDialog.setMessage("Enter Username or Password !!!");
	        	      alertDialog.setIcon(R.drawable.colombuslogo);
	        	      alertDialog.setButton("OK", new android.content.DialogInterface.OnClickListener() {			
	        				public void onClick(DialogInterface arg0, int arg1) {
	        					// TODO Auto-generated method stub
	        					arg0.cancel();
	        				}
	        			});
	        	      alertDialog.show();
	        	}
	        	else
	        	{
	        		//new performBackgroundTask().execute();
	        		Login();
	        	}
			}
		});
        
        et_accountID=(EditText)findViewById(R.id.editText1);
        et_username=(EditText)findViewById(R.id.editText2);
        et_password=(EditText)findViewById(R.id.editText3);
        
    }

    public void Login()
    {      	
        	String username = et_username.getText().toString();   
        	String password = et_password.getText().toString();
        	String accountID = et_accountID.getText().toString();
        	
        	System.out.println(username);
        	System.out.println(password);
        	System.out.println(accountID);
        	
        	GpsData gpsData = new GpsData();
        	gpsData.setUsername(username);
        	gpsData.setPassword(password);
        	gpsData.setAccountID(accountID);
        	
        	if(gpsData.Login())
        	{
        		System.out.println("Result : "+gpsData.Login());
        		System.out.println("back in login "+gpsData.getUserType());
				ColombusApp.getSharedApp().accountId = accountID;
					System.out.println("Condition : Main User");
					Intent intent = new Intent(LoginActivity.this,MainUserList.class);
					
    				intent.putExtra("GpsData", gpsData);
    				LoginActivity.this.startActivity(intent); 
    				//overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
    				finish();
		    }
				
        	
        	else
        	{ 
                result=gpsData.getErrorMsg();
        		System.out.println("Status message : "+result); 
        		Toast.makeText(LoginActivity.this,result+"!!!", Toast.LENGTH_LONG).show();
        		
        	}
    }
    
   

    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK))
	    {
	    	
	    	   Log.i("HA", "Finishing");
	    	   Intent intent = new Intent(Intent.ACTION_MAIN);
	    	   intent.addCategory(Intent.CATEGORY_HOME);
	    	   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	   startActivity(intent);
	    }
	    return super.onKeyDown(keyCode, event);
	}
    
    
}